package com.goodomen.garbage.web.util;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static java.time.DayOfWeek.SATURDAY;
import static java.time.DayOfWeek.SUNDAY;

public class TradeTimeUtil {
    /**
     * 2019 年的节假日
     */
    private static final List<LocalDate> HOLIDAY = new ArrayList<>();
    static {
        LocalDate duanWu = LocalDate.of(2019, 6, 7);
        LocalDate zhongQiu = LocalDate.of(2019, 9, 13);
        LocalDate guoQin1 = LocalDate.of(2019, 10, 1);
        LocalDate guoQin2 = LocalDate.of(2019, 10, 2);
        LocalDate guoQin3 = LocalDate.of(2019, 10, 3);
        LocalDate guoQin4 = LocalDate.of(2019, 10, 4);
        LocalDate guoQin7 = LocalDate.of(2019, 10, 7);
        HOLIDAY.add(duanWu);
        HOLIDAY.add(zhongQiu);
        HOLIDAY.add(guoQin1);
        HOLIDAY.add(guoQin2);
        HOLIDAY.add(guoQin3);
        HOLIDAY.add(guoQin4);
        HOLIDAY.add(guoQin7);
    }

    private TradeTimeUtil() {}

    /**
     *
     * @return  是否是交易时间
     */
    public static boolean isTradeTime() {
        LocalDateTime now = LocalDateTime.now();
        DayOfWeek dayOfWeek = now.getDayOfWeek();
        // 先判断是否是周末
        if (dayOfWeek == SATURDAY || dayOfWeek == SUNDAY) {
            return false;
        }
        // 判断是否是节假日
        if (HOLIDAY.contains(now.toLocalDate())) {
            return false;
        }

        int hour = now.getHour();
        int minute = now.getMinute();
        if (hour <= 8 || hour >= 15 || hour == 12) {
            return false;
        }
        if ((hour == 9 && minute <30) || (hour == 11 && minute > 30) || (hour == 14 && minute >= 53)) {
            return false;
        }
        return true;
    }

    /**
     *
     * @return  是否是交易日
     */
    public static boolean isTradeDay() {
        LocalDateTime now = LocalDateTime.now();
        DayOfWeek dayOfWeek = now.getDayOfWeek();
        // 先判断是否是周末
        if (dayOfWeek == SATURDAY || dayOfWeek == SUNDAY) {
            return false;
        }
        // 判断是否是节假日
        if (HOLIDAY.contains(now.toLocalDate())) {
            return false;
        }
        return true;
    }

}
